package com.bw.module_device.view

import android.content.Intent
import android.view.View
import android.widget.PopupMenu
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity
import com.bw.module_device.R
import com.bw.module_device.adapter.AddPicAdapter
import com.bw.module_device.databinding.ActivityAddDeviceBinding
import com.bw.module_device.entity.DeviceResult
import com.bw.module_device.viewmodel.DeviceViewModel
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
/**
 * 智慧园区-01-设备管理-v1.1-吴熙-20250509
 */
@Route(path = RouterPath.PATH_ADD_DEVICE_ACTIVITY)
class AddDeviceActivity : BaseMvvmActivity<ActivityAddDeviceBinding>() {

    private var typeString=""
    private var addressString=""
    private var timeString=""

    lateinit var addPicAdapter: AddPicAdapter

    private val viewModel by lazy { ViewModelProvider(this)[DeviceViewModel::class.java] }


    override fun observeDataChange() {
        viewModel.addDeviceSuccess.observe(this){
            ToastUtils.showLong("提交成功")
            finish()
        }
    }

    override fun initView() {

        //添加图片
        addPicAdapter= AddPicAdapter()
        mDataBinding.rv.layoutManager=
            LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false)
        mDataBinding.rv.adapter=addPicAdapter
        addPicAdapter.data.add("+")
        addPicAdapter.notifyDataSetChanged()

        //头布局
        mDataBinding.title.text="新增检查工单"
        mDataBinding.goMore.visibility=View.VISIBLE
        mDataBinding.goAdd.visibility=View.GONE
        mDataBinding.goBack.setOnClickListener { finish() }

        //工单类型
        mDataBinding.ivShowPop1.setOnClickListener{
            val list= mutableListOf<String>()
            list.add("维修设备")
            list.add("投诉设备")
            list.add("保洁设备")
            list.add("其它设备")
            val optionPicker = OptionPicker(this)
            optionPicker.setTitle("请选择设备类型")
            optionPicker.setData(list)
            optionPicker.setDefaultPosition(0)//设置默认选中
            optionPicker.setOnOptionPickedListener { position, item ->
                typeString=list[position]
                mDataBinding.edtType.setText(typeString)
            }
            optionPicker.show()
        }

        //详细地址
        mDataBinding.ivShowPop2.setOnClickListener {
            val popupMenu= PopupMenu(this,it)
            popupMenu.menuInflater.inflate(R.menu.menu_add_address,popupMenu.menu)
            popupMenu.setOnMenuItemClickListener {
                when(it.itemId){
                    R.id.address1->{
                        addressString=it.title.toString()
                        mDataBinding.edtAddress.setText(addressString)
                        true
                    }
                    R.id.address2->{
                        addressString=it.title.toString()
                        mDataBinding.edtAddress.setText(addressString)
                        true
                    }
                    R.id.address3->{
                        addressString=it.title.toString()
                        mDataBinding.edtAddress.setText(addressString)
                        true
                    }
                    R.id.address4->{
                        addressString=it.title.toString()
                        mDataBinding.edtAddress.setText(addressString)
                        true
                    }
                    else -> true
                }
            }
            popupMenu.show()
        }

        //预约时间
        mDataBinding.ivShowPop3.setOnClickListener {
            val datePicker = DatePicker(this)
            datePicker.setTitle("请选择日期")
            datePicker.wheelLayout.setRange(DateEntity.target(2025,1,1), DateEntity.target(2025,12,31))//设置日期范围
            datePicker.wheelLayout.setDefaultValue(DateEntity.today())//设置默认选中时间
            datePicker.setOnDatePickedListener { year, month, day ->
                timeString="${year}.${month}.${day}"
                mDataBinding.edtTime.setText(timeString)
            }
            datePicker.show()
        }

        //提交
        mDataBinding.btnSunmit.setOnClickListener {
            viewModel.addDevice(
                DeviceResult(
                id=0,
                deviceName = typeString+"A",
                deviceType = typeString,
                failureRate = 0.2,
                installationLocation = addressString,
                status = "正常"
            )
            )
        }

        //图片选择
        mDataBinding.tvAddPicture.setOnClickListener {
            PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE)
                .selectPicture()
        }

        //点击加号添加图片
        addPicAdapter.setOnItemClickListener { adapter, view, position ->
            if (addPicAdapter.data[position].equals("+")){
                PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE)
                    .selectPicture()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            val bundle = data!!.extras
            val bean = bundle!![PictureSelector.PICTURE_RESULT] as PictureBean?
            mDataBinding.tvAddPicture.visibility=View.GONE
            mDataBinding.rv.visibility=View.VISIBLE
            addPicAdapter.data.add(0,bean!!.path)
            addPicAdapter.notifyDataSetChanged()
        }
    }
    override fun getLayoutId(): Int = R.layout.activity_add_device


}